<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="abcList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="合同编号" align="center" prop="contNo" width="180" />

      <el-table-column label="a代码" align="center" prop="acode" />

      <el-table-column label="b代码" align="center" prop="bcode" />

      <el-table-column label="c代码" align="center" prop="ccode" />

      <el-table-column label="类别名称" align="center" prop="name" width="180" />

      <el-table-column label="描述" align="center" prop="mateDesc" width="180" />

      <el-table-column label="规格" align="center" prop="specs" />

      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column label="区域" align="center" prop="region" />

      <el-table-column label="材质" align="center" prop="mateQuality" />

      <el-table-column label="单位" align="center" prop="unit" />

      <el-table-column label="数量" align="center" prop="num" />

      <el-table-column label="AB项成本单价" align="center" prop="abUnitPrice" width="120" />

      <el-table-column label="AB项成本总价" align="center" prop="abTotalPrice" width="120" />

      <el-table-column label="AB项税率" align="center" prop="abTax">
        <template slot-scope="scope">
          <span>{{ scope.row.abTax * 100 }}%</span>
        </template>
      </el-table-column>

      <el-table-column label="AB项进项税" align="center" prop="abInputTax" width="120" />

      <el-table-column label="C项成本单价" align="center" prop="cunitPrice" width="120" />

      <el-table-column label="C项成本总价" align="center" prop="ctotalPrice" width="120" />

      <el-table-column label="C项税率" align="center" prop="ctax">
        <template slot-scope="scope">
          <span>{{ scope.row.ctax * 100 }}%</span>
        </template>
      </el-table-column>

      <el-table-column label="C项进项税" align="center" prop="cinputTax" width="120" />

      <el-table-column label="更新人" align="center" prop="modifyId" />

      <el-table-column label="更新时间" align="center" prop="modifyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="status" />

      <el-table-column label="是否删除" align="center" prop="isDel" /> -->

      <el-table-column label="创建人名称" align="center" prop="createName" width="120"s />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['bid:abc:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['bid:abc:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getListABC" />

    <!-- 添加或修改实施预算-投标-实施abc对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="formABC" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同编号" prop="contNo">
              <el-select v-model="formABC.contNo" placeholder="请选择合同编号" :disabled="isReadOnly" style="width:260px">
                <el-option v-for="item in salecontList" :key="item.contractNo" :label="item.contractNo"
                  :value="item.contractNo"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="a代码" prop="acode">
              <el-select v-model="formABC.acode" placeholder="请选择a代码" :disabled="isReadOnly" style="width:260px">
                <el-option v-for="item in codeListA" :key="item.code" :label="item.code" :value="item.code"></el-option>
              </el-select>
              <!-- <el-input v-model="formABC.aCode" placeholder="请输入a代码" /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="b代码" prop="bcode">
              <el-select v-model="formABC.bcode" placeholder="请选择b代码" :disabled="isReadOnly" style="width:260px">
                <el-option v-for="item in codeListB" :key="item.code" :label="item.code" :value="item.code"></el-option>
              </el-select>
              <!-- <el-input v-model="formABC.bCode" placeholder="请输入b代码" /> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="c代码" prop="ccode">
              <el-select v-model="formABC.ccode" placeholder="请选择c代码" :disabled="isReadOnly" style="width:260px">
                <el-option v-for="item in codeListC" :key="item.code" :label="item.code" :value="item.code"></el-option>
              </el-select>
              <!-- <el-input v-model="formABC.cCode" placeholder="请输入c代码" /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别名称" prop="name">
              <el-input v-model="formABC.name" placeholder="请输入类别名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="描述" prop="mateDesc">
              <el-input v-model="formABC.mateDesc" placeholder="请输入描述" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="规格" prop="specs">
              <el-input v-model="formABC.specs" placeholder="请输入规格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formABC.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="区域" prop="region">
              <el-input v-model="formABC.region" placeholder="请输入区域" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="材质" prop="mateQuality">
              <el-input v-model="formABC.mateQuality" placeholder="请输入材质" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="formABC.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量" prop="num">
              <el-input type="number" v-model="formABC.num" placeholder="请输入数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="AB项成本单价" prop="abUnitPrice">
              <el-input type="number" v-model="formABC.abUnitPrice" placeholder="请输入AB项成本单价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="AB项成本总价" prop="abTotalPrice">
              <el-input type="number" v-model="formABC.abTotalPrice" placeholder="请输入AB项成本总价" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="AB项税率" prop="abTax">
              <!-- <el-select ref="itemNoRef" v-model="formABC.abTax" placeholder="请选择AB项税率" :disabled="isReadOnly"
                style="width: 260px;">
                <el-option v-for="item in taxRateList" :key="item.value" :label="item.label"
                  :value="item.value"></el-option>
              </el-select> -->
              <el-input type="number" v-model="formABC.abTax" placeholder="请输入AB项税率" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="AB项进项税" prop="abInputTax">
              <!-- <el-select ref="itemNoRef" v-model="formABC.abInputTax" placeholder="请选择AB项进项税" :disabled="isReadOnly"
                style="width: 260px;">
                <el-option v-for="item in taxRateList" :key="item.value" :label="item.label"
                  :value="item.value"></el-option>
              </el-select> -->
              <el-input type="number" v-model="formABC.abInputTax" placeholder="请输入AB项进项税" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="C项成本单价" prop="cunitPrice">
              <el-input type="number" v-model="formABC.cunitPrice" placeholder="请输入C项成本单价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="C项成本总价" prop="ctotalPrice">
              <el-input type="number" v-model="formABC.ctotalPrice" placeholder="请输入C项成本总价" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="C项税率" prop="ctax">
              <el-select ref="itemNoRef" v-model="formABC.ctax" placeholder="请选择C项税率" :disabled="isReadOnly"
                style="width: 260px;">
                <el-option v-for="item in taxRateList" :key="item.value" :label="item.label"
                  :value="item.value"></el-option>
              </el-select>
              <!-- <el-input type="number" v-model="formABC.cTax" placeholder="请输入C项税率" /> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="C项进项税" prop="cinputTax">
              <!-- <el-select ref="itemNoRef" v-model="formABC.cinputTax" placeholder="请选择C项进项税" :disabled="isReadOnly"
                style="width: 260px;">
                <el-option v-for="item in taxRateList" :key="item.value" :label="item.label"
                  :value="item.value"></el-option>
              </el-select> -->
              <el-input type="number" v-model="formABC.cinputTax" placeholder="请输入C项进项税" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 实施预算-投标-实施abc导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" action="" :http-request="importData"
        :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的实施预算-投标-实施abc数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAbc, getAbc, delAbc, addAbc, updateAbc, disableAbc } from "@/api/bid/abc";
import { getToken } from "@/utils/auth";
import { downBlobFile } from "@/utils/ruoyi";
import axios from 'axios';

export default {
  name: "Abc",
  props: ["codeListA", "codeListB", "codeListC", "taxRateList", "contNo"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实施预算-投标-实施abc表格数据
      abcList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contNo: this.contNo,
        name: null,
        busType: 1
      },
      // 表单参数
      formABC: {},
      // 表单校验
      rules: {
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的客户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/bid/abc/importData"
      }
    };
  },
  created() {
    this.getListABC();
  },
  methods: {
    /** 查询实施预算-投标-实施abc列表 */
    getListABC() {
      // alert()
      this.loading = true;
      listAbc(this.queryParams).then(response => {
        this.abcList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.formABC = {
        id: null,
        createId: null,
        createTime: null,
        modifyId: null,
        modifyTime: null,
        status: null,
        isDel: null,
        createName: null,
        contNo: null,
        aCode: null,
        bCode: null,
        cCode: null,
        name: null,
        mateDesc: null,
        specs: null,
        remark: null,
        region: null,
        mateQuality: null,
        unit: null,
        num: null,
        abUnitPrice: null,
        abTotalPrice: null,
        abTax: null,
        abInputTax: null,
        cUnitPrice: null,
        cTotalPrice: null,
        cTax: null,
        cInputTax: null,
        busType: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getListABC();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.$emit('change', this.ids);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getAbc(id).then(response => {
        this.open = true;
        this.title = "添加实施预算-投标-实施abc";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAbc(id).then(response => {
        this.formABC = response.data;
        this.open = true;
        this.title = "修改实施预算-投标-实施abc";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.formABC.id != null) {
            updateAbc(this.formABC).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getListABC();
            });
          } else {
            addAbc(this.formABC).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getListABC();
            });
          }
        }
      });
    },
     /** 批量删除按钮操作 */
     handleBatchDelete(row) {
      const ids = this.ids;
      this.$modal.confirm('是否确认删除实施预算-投标-实施abc编号为"' + ids + '"的数据项？').then(function () {
        return delAbc(ids);
      }).then(() => {
        this.getListABC();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除实施预算-投标-实施abc编号为"' + ids + '"的数据项？').then(function () {
        return delAbc(ids);
      }).then(() => {
        this.getListABC();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bid/abc/export', {
        ...this.queryParams
      }, `实施预算-投标-实施abc_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "客户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('erp/customer/importTemplate', {
      }, `实施预算-投标-实施abc_template_${new Date().getTime()}.xlsx`)
    },
    importData(file) {
      const loading = this.$loading({
        lock: true,
        text: '数据导入中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      let formData = new FormData();
      formData.append('file', file.file);
      return axios.post(this.upload.url + '?updateSupport=' + this.upload.updateSupport, formData, {
        headers: this.upload.headers,
        responseType: 'blob'
      }).then(res => {
        loading.close()
        this.handleFileSuccess(res, file);
      }).catch(err => {
        loading.close()
      })
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      if (res.data.type === 'application/json') {
        let reader = new FileReader()
        reader.readAsText(res.data);
        reader.onloadend = () => {
          let rsp = JSON.parse(reader.result);
          if (rsp.code == 200) {
            this.$modal.msgSuccess(rsp.msg);
            this.getListABC();
          } else {
            this.$modal.msgError(rsp.msg);
          }
        }
      } else {
        this.$modal.msgError("导入错误,请查看回执文件");
        let fileName = file.file.name;
        if (fileName.lastIndexOf(".xls") == fileName.length - 4) {
          fileName = fileName.substring(0, fileName.lastIndexOf(".xls"))
        } else if (fileName.lastIndexOf(".xlsx") == fileName.length - 5) {
          fileName = fileName.substring(0, fileName.lastIndexOf(".xlsx"))
        }
        downBlobFile(downBlobFile(res.data, fileName + "Back.xlsx"));
      }
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
